package cn.cuilan.aspect;

import cn.cuilan.utils.ErrorCode;
import cn.cuilan.utils.Results;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * SpringMVC通用异常处理
 *
 * @author zhang.yan
 */
@RestControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    /**
     * 异常同一处理方法，拦截所有异常
     *
     * @return {"errorCode": "100006", "message": "服务器异常", "result": null}
     */
    @ExceptionHandler(value = Exception.class)
    public Results exceptionHandle(Exception e) {
        // 使用 org.apache.commons.lang3.exception.ExceptionUtils 记录异常日志
        logger.error(ExceptionUtils.getStackTrace(e));
        return Results.error().result(ErrorCode.SERVER_ERROR);
    }

}
